package com.weedsdream.app.floatWindow

import android.content.Intent
import android.net.Uri
import android.provider.Settings
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.weedsdream.BaseActivity
import com.weedsdream.app.R
import com.weedsdream.app.colorplate.curRGB
import com.weedsdream.app.compose.moreUiOpen
import com.weedsdream.app.compose.scCenter
import com.weedsdream.function.ToastBtn
import com.weedsdream.function.toast

/**悬浮窗功能开启状态*/
var floatWindow by mutableStateOf(false)

/**底层浮窗透明度:0-100*/
var floatWindowAlpha by mutableIntStateOf(80)

/**启用浮动画板*/
var enableFloatBoard by mutableStateOf(false)

/**悬浮窗的设置Ui*/
@Preview(widthDp = 200, heightDp = 350)
@Composable
internal fun FloatWindowSetUi(test: Boolean = true) {
    Column(Modifier.padding(5.dp)) {
        Row(
            Modifier
                .align(Alignment.CenterHorizontally)
                .pointerInput(Unit) {
                    detectTapGestures {
                        var success = true//是否成功执行
                        if (floatWindow) FloatingWindowActivity.destroyFloatWindow()
                        else FloatingWindowActivity.openFloatWindow {
                            success = false
                            toast(
                                "由于系统限制,悬浮窗功能需要您授予权限.", "权限请求", null,
                                listOf(ToastBtn.cancel(), ToastBtn.ensure {
                                    val intent = Intent(
                                        Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                                        Uri.parse("package:${BaseActivity.activity.packageName}")
                                    )
                                    RequestPermissionLauncher.floatingWindow!!.launch(intent)
                                    true
                                })
                            )
                        }
                        if (success) {//变量重置
                            scCenter = null
                            floatWindow = !floatWindow
                            moreUiOpen = false
                            enableFloatBoard = false
                        }
                    }
                }) {
            Text(
                text = "启用悬浮窗", Modifier.align(Alignment.CenterVertically), fontSize = 15.sp,
                color = if (floatWindow) Color.Cyan else Color.Black
            )
            Icon(
                painterResource(
                    id = if (floatWindow) R.drawable.select_yes else R.drawable.select_no
                ), null, Modifier
                    .padding(start = 5.dp)
                    .size(25.dp)
                    .align(Alignment.CenterVertically),
                if (floatWindow) Color.Blue else Color.Black
            )
        }
        if (test || floatWindow) Column(Modifier.align(Alignment.CenterHorizontally)) {
            Text(
                text = "透明度:${floatWindowAlpha}%", Modifier
                    .padding(vertical = 5.dp)
                    .align(Alignment.CenterHorizontally),
                fontSize = 14.sp
            )
            Box(Modifier
                .fillMaxHeight()
                .width(45.dp)
                .align(Alignment.CenterHorizontally)
                .pointerInput(Unit) {//滑动时重设笔迹
                    detectDragGestures { change, _ ->
                        setWindowAlpha(change.position, size.height)
                    }
                }
                .pointerInput(Unit) {//触摸时重设笔迹
                    detectTapGestures(onPress = { setWindowAlpha(it, size.height) })
                }) {
                //透明度示范层
                val lineSize = 50 //线条总数(包含纯透明)
                Canvas(
                    Modifier
                        .fillMaxHeight()
                        .fillMaxWidth(0.5f)
                        .align(Alignment.Center)
                ) {
                    val strokeWidth = size.height / lineSize
                    for (i in 0 until lineSize) (i.toFloat() / lineSize).also {
                        val y = size.height * it + strokeWidth / 2
                        val alpha = 255 - (255 * it).toInt()
                        drawLine(
                            Color(0, 0, 0, alpha), Offset(0f, y), Offset(size.width, y), strokeWidth
                        )
                    }
                }
                //透明度选择位置线
                Canvas(
                    Modifier
                        .fillMaxHeight()
                        .fillMaxWidth(0.8f)
                        .align(Alignment.Center)
                ) {
                    val y = size.height * (100 - floatWindowAlpha) / 100
                    drawLine(curRGB, Offset(0f, y), Offset(size.width, y), 5f)
                }
            }
        }
    }
}

/**根据触摸位置设置浮窗透明度
 * @param pos 触摸位置
 * @param height 触摸区域高度*/
private fun setWindowAlpha(pos: Offset, height: Int) {
    floatWindowAlpha = (100 - (100 * pos.y / height)).toInt().let {
        if (it < 0) 0 else if (it > 100) 100 else it
    }
}
